# -*- coding:utf-8 -*-
# Author:Ami
# @Time    : 2018/12/9 9:21

import socket, os, time,hashlib

server = socket.socket()
server.bind(('localhost', 9999))

server.listen()
while True:
    conn, addr = server.accept()
    print("new conn:", addr)
    while True:
        print("等待新指令")
        data = conn.recv(1024)
        if not data:
            print("客户端已断开")
            break
        cmd, filename = data.decode().split()
        print(filename)
        if os.path.isfile(filename):  # 判断是否是文件
            f = open(filename+".new", 'rb')
            m = hashlib.md5()
            file_size = os.stat(filename).st_size
            conn.send(str(filename).encode())#发送文件大小
            conn.recv(1024)#等待回应
            for line in f:
                m.update(line)
                conn.send(line)
            # print("file md5",m.hexdigest())
            f.close()
    print("send done")
server.close()
